//
//  OfflineController.swift
//  I.S.A.P WT
//
//  Created by Илья Проскурнев on 18.05.2020.
//  Copyright © 2020 Илья Проскурнев. All rights reserved.
//

import SwiftUI
import AVFoundation
import AudioToolbox

class OfflineController: UIViewController {
    
    @IBOutlet weak var minus01: UIButton!
    @IBOutlet weak var minus03: UIButton!
    
    var pr1: Int!
    var pr2: Int!
    var pr3: Int!
    
    @IBOutlet weak var can03: UIButton!
    @IBOutlet weak var res: UIButton!
    @IBOutlet weak var can01: UIButton!
    @IBOutlet weak var err01: UILabel!
    @IBOutlet weak var err03: UILabel!
    @IBOutlet weak var presentation: UIButton!
    
    @IBOutlet weak var accuracy: UILabel!
    var doubleAccuracy: Double?
    var setAccuracy: Int?
    var num03 = 0
    var num01 = 0
    var Acd = Double(0)
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.navigationItem.setHidesBackButton(true, animated: true)
            
            // Do any additional setup after loading the view.
            can03.layer.cornerRadius = 3
            can03.layer.borderWidth = 3
            can03.layer.borderColor = UIColor.gray.cgColor
    
            can01.layer.cornerRadius = 3
            can01.layer.borderWidth = 3
            can01.layer.borderColor = UIColor.gray.cgColor
    
            res.layer.cornerRadius = 3
            res.layer.borderWidth = 3
            res.layer.borderColor = UIColor.gray.cgColor
    
            printScoring(String(accuracy.text! as NSString))
        }
    
    override func prepare (for segue: UIStoryboardSegue, sender: Any?) {
        let presentationController: OfflinePresentation = segue.destination as! OfflinePresentation
        presentationController.Acd = self.Acd
        presentationController.num01 = self.num01
        presentationController.num03 = self.num03
    }
    
    @discardableResult
    func printScoring(_: String) -> String {
        
        if(num03 == 0) {
            err03.text = "0 X"
        } else {
            err03.text = String(num03) + " X"
        }
        
        if(num01 == 0) {
            err01.text = "X 0"
        } else {
            err01.text = "X " + String(num01)
        }
        
        let acc = 40 - num01 - (num03 * 3)
        if (acc<0) {
            Acd = 0
        } else {
            Acd = (Double(acc)/Double(10))
        }
        let sc = String(Acd)
        accuracy.text = sc
        return sc
    }
    
    @IBAction func cancel03(_ sender: Any) {
        if (num03>0) { num03=num03-1 }
        printScoring(String(accuracy.text! as NSString))
    }
    @IBAction func reset(_ sender: Any) {
        num01 = 0
        num03 = 0
        printScoring(String(accuracy.text! as NSString))
    }
    @IBAction func cancel01(_ sender: Any) {
        if (num01>0) { num01=num01-1 }
        printScoring(String(accuracy.text! as NSString))
    }
    @IBAction func btn03(_ sender: UIButton) {
        if(UIDevice().userInterfaceIdiom == .phone)
        {
            AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
        }
        else
        {
            AudioServicesPlayAlertSound (1105);
        }
        if (num01+num03*3 < 40) {
            num03 = num03+1
        }
        printScoring(String(accuracy.text! as NSString))
    }
    @IBAction func btn01(_ sender: UIButton) {
        AudioServicesPlaySystemSound(SystemSoundID(1003))
        if (num01+num03*3 < 40) {
            num01 = num01+1
        }
        printScoring(String(accuracy.text! as NSString))
    }
}
